package com.jiayou.activity.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BitmapFactory
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.hutool.http.HttpRequest
import com.jiayou.R
import com.jiayou.activity.ChatActivity
import com.jiayou.activity.MainActivity
import com.qmuiteam.qmui.widget.QMUIRadiusImageView
import de.hdodenhof.circleimageview.CircleImageView
import kotlinx.android.synthetic.main.menu_item.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread

class MessageAdapter(var context: Context, var friend: String, var list: ArrayList<ChatMessage>) :
    RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {
    private var isLoad1 = false
    private var isLoad2 = false
    private var bytes1: ByteArray? = null
    private var bytes2: ByteArray? = null


    class MessageViewHolder(
        view: View
    ) : RecyclerView.ViewHolder(view) {
        var msg_view: LinearLayout? = null
        var msg: TextView? = null
        var img1: QMUIRadiusImageView? = null
        var img2: QMUIRadiusImageView? = null

        init {
            msg_view = view as LinearLayout
            msg = view.findViewById(R.id.msg)
            img1 = view.findViewById(R.id.touxiang1)
            img2 = view.findViewById(R.id.touxiang2)

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
        println(viewType)
        return MessageViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.chat_item,
                parent,
                false
            )
        )
    }

    override fun getItemCount() = list.size

    @SuppressLint("RtlHardcoded")
    override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
        val user = list[position]
        if (user.id1 == friend) {
            holder.msg?.setBackgroundResource(R.drawable.im_msg_left)
            holder.msg?.gravity = Gravity.CENTER_VERTICAL
            if (!isLoad1) {
                isLoad1 = true
                thread {
                    bytes1 = HttpRequest.get("http://${(context as ChatActivity).uri}/im/userinfo/getImgById")
                        .form("id", friend)
                        .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                        .execute()
                        .bodyBytes()
                    runBlocking {
                        withContext(Dispatchers.Main) {
                            holder.img1?.setImageBitmap(BitmapFactory.decodeByteArray(bytes1, 0, bytes1!!.size))
                        }
                    }
                }
            } else {
                if (bytes1 != null)
                    holder.img1?.setImageBitmap(BitmapFactory.decodeByteArray(bytes1, 0, bytes1!!.size))
            }
        } else {
            holder.msg?.setBackgroundResource(R.drawable.im_msg_right)
            holder.msg?.gravity = Gravity.CENTER_VERTICAL
            if (!isLoad2) {
                isLoad2 = true
                thread {
                    bytes2 = HttpRequest.get("http://${(context as ChatActivity).uri}/im/userinfo/getImgById")
                        .form("id", MainActivity.ID)
                        .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                        .execute()
                        .bodyBytes()
                    runBlocking {
                        withContext(Dispatchers.Main) {
                            holder.img2?.setImageBitmap(BitmapFactory.decodeByteArray(bytes2, 0, bytes2!!.size))
                            isLoad2 = true
                        }
                    }
                }
            } else {
                if (bytes2 != null)
                    holder.img2?.setImageBitmap(BitmapFactory.decodeByteArray(bytes2, 0, bytes2!!.size))
            }

        }
        holder.msg?.text = user.text
    }

    override fun getItemViewType(position: Int) = position
}